import requests
import json

from django.conf import settings


# from .exception import ValidationError


class MyRequests:
    def __init__(self):
        self.userName = 'admin'
        self.password = 'public'
        self.headers = {
            'content-type': 'application/json',
            'User-Agent': '0.0.1',
            "application/json;charset": "utf-8",
        }

    def get_devices(self, order_id, start_num):
        url = "http://120.36.152.254:21040/api/devices/peizhi/?order_id={}"
        url_front = url.format(order_id)
        r = requests.get(url_front, headers=self.headers)
        status_code = r.status_code
        if status_code == 200:
            content = r.content.decode("utf-8")
            content = json.loads(content)
            huixie_results = []
            results = content.get("results")
            for r in results:
                sql_id = r.get("sql_id")
                start_num += 1
                device_id = str(start_num)
                mcu_id = "123141421314141421"
                huixie_results.append({
                    "sql_id": sql_id,
                    "device_id": device_id,
                    "mcu_id": mcu_id
                })
            self.huixie_devices(huixie_results)

    def huixie_devices(self, data):
        url_front = "http://120.36.152.254:21040/api/devices/huixie/"
        r = requests.post(url_front, data=json.dumps({"sn_list": data}), headers=self.headers)
        status_code = r.status_code
        if status_code == 200:
            content = r.content.decode("utf-8")
            content = json.loads(content)


my_request = MyRequests()
my_request.get_devices("DD232309210002", 80000000)
